import sys
import re
#try
# import pygtk
# import gobject, gtk
# import gtk.glade
# import gnome.ui
# import gobject
#except:

try:
    import gtk
    have_gtk = True
except:
    print 'gtk not availible'
    have_gtk = False
    
class ProgressReporter():
    
    def __init__(self, main_gui = None):
        """ Reports progress of operations
        
        This class decouples the MainGUI class from the backup_backend.Backup class
        If needed, It could be made into a factory at a later stage
        
        main_gui
            An instance of MainGUI
        
        """
        self.main_gui = main_gui
        
        if self.main_gui and have_gtk:
            self.xml = main_gui.xml
            
        else:
            self.xml = None
            return
        
        self.progress_bar = self.xml.get_widget('progress_bar')
        
        self.text_view = self.xml.get_widget('backup_output_text')
        self.text_buffer = self.text_view.get_buffer()
        
        self.backup_button = self.xml.get_widget('backup_button')
        
        self.restore_button = self.xml.get_widget('restore_button')

        
        self.status_bar = self.xml.get_widget('status_bar')
        self.status_bar_context_ids = []
                
    def set_description(self, desc):
        if not self.main_gui:
            print "operation : " + desc
            return
        
        gtk.gdk.threads_enter()

        self.progress_bar.set_text(desc)
        self.progress_bar.pulse()

        gtk.gdk.threads_leave()
        

    def set_progress(self, percent):
        if not self.main_gui:
            return
        
        gtk.gdk.threads_enter()

        self.progress_bar.set_fraction(percent)
        
        gtk.gdk.threads_leave()
    
    def append_output(self, output):
        output = re.sub('[\n]$', '', output)
        
        if not self.main_gui:
            print "output : " + desc
            return
        
        gtk.gdk.threads_enter()

        self.text_buffer.insert( self.text_buffer.get_end_iter(), output +'\n' )
        self.text_view.scroll_to_mark(self.text_buffer.get_insert(), 0.499)
        self.progress_bar.pulse()
        
        gtk.gdk.threads_leave()
        
    def clear_output(self):
        self.text_buffer.delete( self.text_buffer.get_start_iter(), self.text_buffer.get_end_iter() )

    def show_error(self, msg):
        """ Shows an error message
        
        This function will display an error message either to the GUI or command line
        
        msg
            The message to display
            
        """
        if not self.main_gui:
            # TODO, output to stderr
            print "error : " + msg
            return
        
        error = gtk.MessageDialog( type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK, flags=gtk.DIALOG_MODAL )
        error.set_markup(msg)
        error.connect('response', lambda x,y: error.destroy())
        error.show()
                
    def push_status_bar_text(self, text):
        if not self.main_gui:
            print "new status : " + text
            return

        gtk.gdk.threads_enter()

        context_id = self.status_bar.get_context_id("performing operation")
        self.status_bar.push(context_id, 'Backup In Progress')
        self.status_bar_context_ids.append(context_id)
        
        gtk.gdk.threads_leave()

    def pop_status_bar_text(self):
        if not self.main_gui:
            print "deleted status : ???"
            return

        gtk.gdk.threads_enter()

        self.status_bar.pop(self.status_bar_context_ids.pop())
        
        gtk.gdk.threads_leave()
        
    def set_backup_in_progress(self, in_progress):
        """ Set backup in progress
        
        Set the state of the backup in progress.
        disable and enable relevent widgits like the backup button
        
        in_progress
            True/False is the backup in progress
            
        """

        if not self.main_gui:
            print "backup in progress : " + in_progress
            return


        if in_progress:
            gtk.gdk.threads_enter()
            self.backup_button.set_label('Backup is running...')
            self.backup_button.set_sensitive(False)
            gtk.gdk.threads_leave()
            
            self.clear_output()
            
            self.push_status_bar_text('Backup In Progress')

        else:
            gtk.gdk.threads_enter()
            self.backup_button.set_label('Backup')
            self.backup_button.set_sensitive(True)
            gtk.gdk.threads_leave()

            self.pop_status_bar_text()

            self.set_progress(0)
            
                
                
    def set_restore_in_progress(self, in_progress):
        """ Set restore in progress
        
        Set the state of the restore in progress.
        disable and enable relevent widgits like the restore button
        
        in_progress
            True/False is the restore in progress
            
        """

        if not self.main_gui:
            print "restore in progress : " + in_progress
            return


        
        if in_progress:
            gtk.gdk.threads_enter()
            self.restore_button.set_label('Restore is running...')
            self.restore_button.set_sensitive(False)
            gtk.gdk.threads_leave()
                        
            self.clear_output()
        
        else:
            gtk.gdk.threads_enter()
            self.restore_button.set_label('Restore')
            self.restore_button.set_sensitive(True)
            gtk.gdk.threads_leave()
            
            